package com.bigmi.article.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bigmi.article.mapper.ArticleConfigMapper;
import com.bigmi.article.mapper.ArticleContentMapper;
import com.bigmi.article.mapper.ArticleMapper;
import com.bigmi.article.service.ArticleFreemarkerService;
import com.bigmi.article.service.ArticleService;
import com.bigmi.common.cont.ArticleCont;
import com.bigmi.model.article.Dto.ArticleDto;
import com.bigmi.model.article.Dto.ArticleHomeDto;
import com.bigmi.model.article.pojo.ApArticle;
import com.bigmi.model.article.pojo.ApArticleConfig;
import com.bigmi.model.article.pojo.ApArticleContent;
import com.bigmi.model.common.dtos.ResponseResult;
import com.bigmi.model.common.enums.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
@Slf4j
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, ApArticle> implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private ArticleConfigMapper articleConfigMapper;

    @Autowired
    private ArticleContentMapper articleContentMapper;

    @Autowired
    private ArticleFreemarkerService articleFreemarkerService;
    /**
     *
     * @param articleHomeDto
     * @param type type = 1加载更多 type = 2加载最新
     * @return
     */
    @Override
    public ResponseResult articleList(ArticleHomeDto articleHomeDto, Short type) {

        Integer size = articleHomeDto.getSize();
        if (size == null || size ==0){
            size=10;
        }
        size=Math.min(size,50);
        articleHomeDto.setSize(size);

        if (!type.equals(ArticleCont.LOADTYPE_LOAD_MORE) && !type.equals(ArticleCont.LOADTYPE_LOAD_NEW)){
            type=1;
        }
        //频道
        String tag = articleHomeDto.getTag();
        if (StringUtils.isEmpty(tag)){
            articleHomeDto.setTag(ArticleCont.DEFAULT_TAG);
        }
        //获取时间
        if (articleHomeDto.getMaxBehotTime()==null){
            articleHomeDto.setMaxBehotTime(new Date());
        }
        if (articleHomeDto.getMinBehotTime()==null){
            articleHomeDto.setMinBehotTime(new Date());
        }


        List<ApArticle> apArticles = articleMapper.articleList(articleHomeDto, type);
        return  ResponseResult.okResult(apArticles);

    }

    /**
     * app端保存文章
     * @param articleDto
     * @return
     */
    @Override
    public ResponseResult saveArticle(ArticleDto articleDto) {
        /*try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        //判断
        if (articleDto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        ApArticle apArticle=new ApArticle();
        BeanUtils.copyProperties(articleDto,apArticle);
        //判断是否存在id
        if (articleDto.getId()==null){
            //不存在id 保存 文章 文章配置 文章内容
            save(apArticle);
            //保存文章配置
            ApArticleConfig apArticleConfig=new ApArticleConfig(apArticle.getId());
            //apArticleConfig.setArticleId(apArticle.getId());
            articleConfigMapper.insert(apArticleConfig);
            //保存文章内容
            ApArticleContent apArticleContent =new ApArticleContent();
            apArticleContent.setArticleId(apArticle.getId());
            apArticleContent.setContent(articleDto.getContent());
            articleContentMapper.insert(apArticleContent);

        }else {
            //存在id 修改 文章 文章内容
            //修改文章
            updateById(apArticle);
            //修改文章内容
            LambdaQueryWrapper<ApArticleContent> queryWrapper =new LambdaQueryWrapper<>();
            LambdaQueryWrapper<ApArticleContent> eq = queryWrapper.eq(ApArticleContent::getArticleId, articleDto.getId());
            ApArticleContent apArticleContent = articleContentMapper.selectOne(eq);
            apArticleContent.setContent(articleDto.getContent());
            articleContentMapper.updateById(apArticleContent);
        }
        //异步调用 生成静态文件上传到minio中
        articleFreemarkerService.buildtoMinio(apArticle,articleDto.getContent());

        //返回结果
        return ResponseResult.okResult(apArticle.getId());
    }

    @Override
    public ResponseResult upAndDown(Long articleId) {

        articleMapper.updataByArticleId(articleId);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult del(Long articleId) {
        articleMapper.delByArticleId(articleId);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
